{% extends "admin/layout_admin.html" %}
{% block content %}
<!-- rich editer -->
<script type="text/javascript" src="/static/jquery-plugin/jquery-1.6.4.js"></script>
<script type="text/javascript" src="/static/markitup/jquery.markitup.js"></script>
<script type="text/javascript" src="/static/markitup/sets/html/set.js"></script>
<link rel="stylesheet" type="text/css" href="/static/markitup/skins/simple/style.css" />
<link rel="stylesheet" type="text/css" href="/static/markitup/sets/html/style.css" />
<link rel="stylesheet" type="text/css" href="/static/uploadify/uploadify.css" />
<script type="text/javascript">
<!--
$(document).ready(function()    {
    // Add markItUp! to your textarea in one line
    $('#html').markItUp(myHtmlSettings);
});
-->
</script>

<!-- rich editer end -->

<!-- facebox -->
<link href="/static/facefiles/facebox.css" media="screen" rel="stylesheet" type="text/css" />
<script src="/static/facefiles/facebox.js" type="text/javascript"></script>

<script type="text/javascript">
    jQuery(document).ready(function($) {
      $('a[rel*=facebox]').facebox()
      //$('a[rel*=facebox]').click(); 
    })
</script>

<!-- /facebox -->

<?py use_default_theme = ('default' == THEME) ?>

<span id="h"></span>
<h2>{{title}}</h2>
<form action="/admin/add_post" method="post" id="commentform">
<p id="text" style="color:red;"></p>

<p>*分类: <input type="text" name="category" id="cat" style="width:150px;" value="" tabindex="1" /><a href="#catdiv" rel="facebox[.thickstyle]">从已有分类中选择</a></p>

<p>*标题: <input type="text" name="title" id="tit" style="width:430px;" value="" tabindex="2" /></p>
<textarea id="html" name="content" style="width:500px;height:300px;" tabindex="3"></textarea>

上传图片或附件 <input id="filetoupload" type="file" name="filetoupload"/>
<p>标签: <input type="text" id="tag" name='tags' style="width:360px;" value="" tabindex="4" />用逗号分隔','<br/>
<a href="#tagdiv" rel="facebox[.thickstyle]">从最近标签中选择</a>
</p>
<p>

<?py if use_default_theme: ?>
 设置阅读密码: <input type="text" name='password' id="password" style="width:150px;" value="" tabindex="6" />
<?py else: ?>
<input type='hidden' value='' id="password" />
<?py #endif ?>

</p>
<p><button id="submit1" class="bigbtn"> 提交文章 </button></p>
</form>

<script>
$('#submit1').click(function(){
    
    var cat = $('#cat').val().replace(/(^\s*)|(\s*$)/g,"");
    var tit = $('#tit').val().replace(/(^\s*)|(\s*$)/g,"");
    var con = $('#html').val().replace(/(^\s*)|(\s*$)/g,"");
    var tag = $('#tag').val().replace(/(^\s*)|(\s*$)/g,"");
    var password = $('#password').val().replace(/(^\s*)|(\s*$)/g,"");
    
    var t = $('#text');
    
    if(cat.length< 1 || cat.length > 17){
        $('#cat').focus();
        t.html("分类 必填 且最多可填17个字符");
        return;
    }
    
    if(tit.length< 1 || tit.length > 100){
        $('#tit').focus();
        t.html("标题 必填 且最多可填100个字符");
        return;
    }
    
    if(con.length< 1){
        document.getElementsByTagName('textarea')[0].focus();
        t.html("内容 必填");
        return;
    }
        
    if(tag.length > 100){
        $('#tag').focus();
        t.html("标签最多可填100个字符");
        return;
    }
    
    if(password.length > 8){
        $('#password').focus();
        t.html("密码最多可设8个字符");
        return;
    }
        
    $('#submit1').attr('disabled', 'disabled');
    t.html("Sending now.....");
	$('#commentform').submit();    
});

function setcat(cat){
    var catField = document.getElementById('cat');
    catField.value = cat;
}

function settag(tag){
    var tagField = document.getElementById('tag');
    tagField.value += tag + ",";
}

</script>

<div id="catdiv" style="display:none;line-height:1.7em;color:#999">
<h4>请选择以下已有分类</h4>
{% for cat in cats %}
<a href="#" onclick="setcat('{{cat.name}}');return false;">{{cat.name}}</a>, 
{% endfor %}
</div>

<div id="tagdiv" style="display:none;line-height:1.7em;color:#999">
<h4>请选择以下已有标签</h4>
{% for tag in tags %}
<a href="#" onclick="settag('{{tag.name}}');return false;">{{tag.name}}</a>, 
{% endfor %}
</div>

<!-- ajaxfileupload -->
<script src="/static/uploadify/swfobject.js" type="text/javascript" charset="utf-8"></script>
<script src="/static/uploadify/jquery.uploadify.v2.1.4.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$('#filetoupload').uploadify({
    'uploader'  : '/static/uploadify/uploadify.swf',
    'script'    : '/uploadFile',
    'cancelImg' : '/static/uploadify/cancel.png',
    'folder'    : '/uploads',
	'fileExt'   : '*.txt;*.pdf;*.png;*.jpg;*.jpeg;*.gif;*.doc;*.rar',
	'fileDesc'  : 'Image,Txt,doc,pdf,rar Files',
	'sizeLimit' : 2048000,//2M
    'auto'      : true,
	'queueID'   : 'fileQueue',
	'multi'     : false, //是否允许同时上传多文件，默认false
	'simUploadLimit' : 1, //一次同步上传的文件数目  
	'queueSizeLimit' : 3, //限制在一次队列中的次数（可选定几个文件）。默认值= 999，而一次可传几个文件有 simUploadLimit属性决定。
	'buttonText'     : 'BROWSE',
	onComplete: function (event, queueID, fileObj, response, data) {
		var el = document.getElementById('html');
        var filetype = fileObj.name.toLowerCase().split(".");
        var tp ="png,jpg,jpeg,gif";
        var rs=tp.indexOf(filetype[filetype.length-1]);
        if(rs>=0){
             el.value += '<p>图片：' + fileObj.name + '<img src="' + response + '" /> </p>';
        }else{
             el.value += '<p>附件：' + fileObj.name + ' <a href="' + response + '" target="_blank">点击下载</a> </p>';
        }
        el.focus();
	}  
	});
</script>
<!-- /ajaxfileupload -->
{% endblock %}